<template>
  <el-row
    type="flex"
    justify="space-between"
    align="middle"
    style="width: 100%; height: 40px"
  >
    <!-- 左边 -->
    <el-col>
      <span>{{ treeData.name }}</span>
    </el-col>
    <!-- 右边 -->
    <el-col :span="10">
      <el-row type="flex" justify="end">
        <el-col>{{ treeData.manager }}</el-col>
        <el-col>
          <!-- 下拉列表 -->
          <el-dropdown>
            <span class="el-dropdown-link">
              操作
              <i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="$emit('openDialog', treeData.id)"
              >
                添加子部门
              </el-dropdown-item>
              <el-dropdown-item
                v-if="!isRoot"
                @click.native="$emit('editDialog', treeData.id)"
              >
                编辑部门
              </el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" @click.native="delDepart">
                删除部门
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartmentApi } from '@/api/departments'
export default {
  name: 'TreeTools',
  props: {
    treeData: {
      type: Object,
      // 这个箭头函数表示返回一个对象
      default: () => ({})
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  // created() {
  //   console.log('treeData', this.treeData)
  // },
  methods: {
    // 删除部门
    async delDepart() {
      try {
        // 弹出框
        await this.$confirm('确认删除吗?')
        // 发请求删除
        await delDepartmentApi(this.treeData.id)
        // 删除成功提示
        this.$message.success('删除成功')
        // 调用父级函数请求重新获取数据
        this.$emit('updateList')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style></style>
